package com.cgq.pro.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.cgq.base.dao.impl.BaseDaoImpl;
import com.cgq.pro.dao.RegionStaticDao;
import com.cgq.pro.entity.LandForm;
import com.cgq.pro.entity.RegionStatistics;

@Repository
public class RegionStaticDaoImpl extends BaseDaoImpl<RegionStatistics>
		implements RegionStaticDao {
	public List<RegionStatistics> getAllRecords(final int start,final int pageSize,
			Map<String, String> map) {
		final StringBuilder hql=new StringBuilder();
		hql.append("from RegionStatistics o where 1=1");
		if(map!=null && map.size()>0){
			hql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			hql.append(" and o.dateCre<='"+map.get("endDate")+"'");
			if(!"-1".equals(map.get("regionid").toString())){
				hql.append(" and regionid = '"+map.get("regionid")+"'");
			}
		}
		List list=null;
		try {
			list=hibernateTemplate.executeFind(new HibernateCallback() {

				public List doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql.toString()); 
					query.setFirstResult(start);
					query.setMaxResults(pageSize);
					return query.list();
				}
			});
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		return list;
	}
	
	public int getTotal(Map<String, String> map) {
		final StringBuilder hql=new StringBuilder();
		hql.append("from RegionStatistics o where 1=1");
		if(map!=null && map.size()>0){
			hql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			hql.append(" and o.dateCre<='"+map.get("endDate")+"'");
			if(!"-1".equals(map.get("regionid").toString())){
				hql.append(" and regionid = '"+map.get("regionid")+"'");
			}
		}
		List list=hibernateTemplate.find(hql.toString());
		if(list!=null && list.size()>0)
			return list.size();
		else 
			return 0;
	}

	public List getStatisticsData(Map<String,String> map) {
		final StringBuilder sql=new StringBuilder();
		sql.append("select month(date_cre) as month,sum(cjts) as ts,sum(cjmj) as mj,sum(cjjj) as jj from fd_regionstatistics where 1=1");
		if(map!=null&&map.size()>0){
			sql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			sql.append(" and o.dateCre<='"+map.get("endDate")+"'");
			if(!"-1".equals(map.get("regionid").toString())){
				sql.append(" and regionid = '"+map.get("regionid")+"'");
			}
		}
		sql.append(" group by month(date_cre)");
		
		List list=hibernateTemplate.executeFind(new HibernateCallback() {
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createSQLQuery(sql.toString());
				return query.list();
			}
		});
		return list;
	}

	public List<RegionStatistics> getAllRecords(LandForm landForm) {
		StringBuffer sb=new StringBuffer();
		sb.append(" from RegionStatistics l where 1=1 ");
		if(landForm!=null){
			if(landForm.getStatus()!=null&&!("-1".equals(landForm.getStatus())))
				sb.append(" and  l.p_status in ("+landForm.getStatus()+") ");
			if(landForm.getArea()!=null&&!("-1".equals(landForm.getArea())))
				sb.append(" and l.regionId.id='"+landForm.getArea()+"' ");
			if(landForm.getWy_type()!=null&&!("6".equals(landForm.getWy_type())))
				sb.append(" and l.type="+landForm.getWy_type()+" ");
			if(landForm.getPrice()!=null&&!("1".equals(landForm.getPrice())))
				sb.append(" and l.cjjj "+landForm.getPrice());
		}
		List list=hibernateTemplate.find(sb.toString());
		return list;
	}

}
